package com.demo2do.fms.admin.service.supplier.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.fms.admin.dao.authority.BranchDao;
import com.demo2do.fms.admin.dao.supplier.AppraisalDao;
import com.demo2do.fms.admin.dao.supplier.AppraisalResultDao;
import com.demo2do.fms.admin.dao.supplier.AppraisalTemplateDao;
import com.demo2do.fms.admin.dao.supplier.AppraisalResultItemDao;
import com.demo2do.fms.admin.entity.search.supplier.AppraisalSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.DataServiceImpl;
import com.demo2do.fms.admin.service.supplier.AppraisalService;
import com.demo2do.fms.entity.appraisal.Appraisal;
import com.demo2do.fms.entity.appraisal.AppraisalResult;
import com.demo2do.fms.entity.appraisal.AppraisalResultItem;
import com.demo2do.fms.entity.authority.Branch;
import com.demo2do.fms.entity.supplier.common.Supplier;

/**
 * @author houtao
 */
@Service("appraisalService")
public class AppraisalServiceImpl extends DataServiceImpl<Appraisal> implements AppraisalService {
	
	@Autowired
    private GenericDaoSupport genericDaoSupport;
    
    @Dao
    private AppraisalDao appraisalDao;
    
    @Dao
    private AppraisalResultDao appraisalResultDao;
    
    @Dao
    private AppraisalTemplateDao appraisalTemplateDao;
    
    @Dao
    private BranchDao branchDao;
    
    @Dao
    private AppraisalResultItemDao appraisalResultItemDao;

    /* (non-Javadoc)
	 * @see com.demo2do.fms.admin.service.supplier.AppraisalService#count(com.demo2do.fms.admin.entity.security.Login)
	 */
	public int count(Login login) {
	    
	    if (login.isSuper() || login.isAdminUser()) {
            
            return appraisalDao.count(true);
            
        } else if (login.isBranchUser()) {
            
            Branch branch = branchDao.load(login.getBranch());
            return appraisalDao.count(branch);
            
        } else if (login.isSponsorUser() || login.isFmsUser()) {
            
            Supplier supplier = genericDaoSupport.load(Supplier.class, login.getSupplierId());
            if (supplier.isSecond()) {
                return 0;
            }
            return appraisalDao.count(supplier);
            
        } else {
            return 0;
        }
	    
	}
	
	/*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.AppraisalSerive#count(com.demo2do.fms.admin.entity.search.supplier.AppraisalSearch)
     */
    public int count(AppraisalSearch search, Login login) {
        
        if (login.isSuper() || login.isAdminUser()) {
            
            search.setAdminCreate(true);
            
        } else if (login.isBranchUser()) {
            
            Branch branch = branchDao.load(login.getBranch());
            search.setBranchId(branch.getId());
            
        } else if (login.isSponsorUser() || login.isFmsUser()) {
            
            Supplier supplier = genericDaoSupport.load(Supplier.class, login.getSupplierId());
            if (supplier.isSecond()) {
                return 0;
            }
            search.setSupplierId(supplier.getId());
            
        } else {
            return 0;
        }
        
    	String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        return genericDaoSupport.count(sentence, parameters);
        
    }

    /* (non-Javadoc)
	 * @see com.demo2do.fms.admin.service.supplier.AppraisalService#list(com.demo2do.fms.admin.entity.security.Login, com.demo2do.core.entity.page.Page)
	 */
	public List<Appraisal> list(Login login, Page page) {
	    
	    page.calculate();
	    
	    if (login.isSuper() || login.isAdminUser()) {
            
            List<Appraisal> appraisals = appraisalDao.list(true, page.getBeginIndex(), page.getEveryPage());
            return appraisals;
            
        } else if (login.isBranchUser()) {
            
            Branch branch = branchDao.load(login.getBranch());
            List<Appraisal> appraisals = appraisalDao.list(branch, page.getBeginIndex(), page.getEveryPage());
            return appraisals;
            
        } else if (login.isSponsorUser() || login.isFmsUser()) {
            
            Supplier supplier = genericDaoSupport.load(Supplier.class, login.getSupplierId());
            List<Appraisal> appraisals = appraisalDao.list(supplier, page.getBeginIndex(), page.getEveryPage());
            return appraisals;
            
        } else {
            return Collections.emptyList();
        }
	    
	}
	
	/*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.AppraisalSerive#list(com.demo2do.fms.admin.entity.search.supplier.AppraisalSearch, com.demo2do.core.entity.page.Page)
     */
    @SuppressWarnings("unchecked")
    public List<Appraisal> list(AppraisalSearch search, Login login, Page page) {
        
        // 1. edit search
        if (login.isSuper() || login.isAdminUser()) {
            
            search.setAdminCreate(true);
            
        } else if (login.isBranchUser()) {
            
            Branch branch = branchDao.load(login.getBranch());
            search.setBranchId(branch.getId());
            
        } else if (login.isSponsorUser() || login.isFmsUser()) {
            
            Supplier supplier = genericDaoSupport.load(Supplier.class, login.getSupplierId());
            search.setSupplierId(supplier.getId());
            
        }
        
        // 2. search
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        
        List<Appraisal> appraisals = genericDaoSupport.searchForList(sentence, parameters, page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return appraisals;
        
    }

    /* (non-Javadoc)
	 * @see com.demo2do.fms.admin.service.supplier.AppraisalService#create(com.demo2do.fms.entity.appraisal.Appraisal, com.demo2do.fms.admin.entity.security.Login)
	 */
	public void create(Appraisal appraisal, Login login) {
	    
	    // 1. valid unique
	    Appraisal existAppraisal = appraisalDao.getByName(appraisal.getName());
	    if (existAppraisal != null) {
	        throw BusinessException.newInstance("500", "已存在同名绩效考核");
	    }
	    
	    // 2. save appraisal
		genericDaoSupport.save(appraisal.initialize(login.getId()));
		
	}

	/* (non-Javadoc)
	 * @see com.demo2do.fms.admin.service.supplier.AppraisalService#edit(com.demo2do.fms.entity.appraisal.Appraisal, com.demo2do.fms.admin.entity.security.Login)
	 */
	public void edit(Appraisal appraisal, Login login) {
	    genericDaoSupport.update(appraisal.operate(login.getId()));
	}
	
	/* (non-Javadoc)
	 * @see com.demo2do.fms.admin.service.supplier.AppraisalService#delete(com.demo2do.fms.entity.appraisal.Appraisal, java.lang.Long)
	 */
	public void delete(Appraisal appraisal, Long operator) {
	    
	    List<AppraisalResult> appraisalResults = appraisalResultDao.list(appraisal);
	    for (AppraisalResult appraisalResult : appraisalResults) {
	        
	        List<AppraisalResultItem> itemResults = appraisalResultItemDao.list(appraisalResult);
            for (AppraisalResultItem itemResult : itemResults) {
                genericDaoSupport.update(itemResult.delete(operator));
            }
            
            genericDaoSupport.update(appraisalResult.delete(operator));
	        
	    }
	    
	    genericDaoSupport.update(appraisal.delete(operator));
	    
	}

	/**
	 * @param appraisal
	 * @param operator
	 */
	public void sign(Appraisal appraisal, Long operator) {
		genericDaoSupport.update(appraisal.finish(operator));
	}

    
    
}